
/*****************************************************************************/
/*	2500 A.D. C Compiler Four Function Calculator Tutorial Program       */
/*****************************************************************************/

	.asm
	.linklist			;update listing after the link
	.symbols			;include all symbols
	.endasm

#define  RECEIVER  0x0230		/* serial port receiver address      */
#define  sprintf nf_int_isprintf	/* use char & int only sprintf       */
#define  sscanf  ns_int_isscanf		/* use char & int only sscanf        */

#include "c6502io.h"			/* include printf prototypes         */

extern void enable_interrupts ();	/* interrupt enable routine          */
extern void disable_interrupts ();	/* interrupt disable routine         */

void line_in (char *);			/* line input routine                */
char upper_case (char);			/* 'upper_case' returns a character  */
char *next_token (char *);		/* 'next_token' returns a char ptr   */
int index;				/* index counter                     */
int operand1, operand2, result;		/* allocate operand #1, operand #2   */
					/* and result storage                */
char input_buffer[30];			/* allocate input buffer             */
char result_buffer[30];			/* allocate result buffer            */
char error_buffer[20];			/* allocate error buffer             */

main ()
{
char operation;				/* storage for operation character   */
char error_flag;			/* storage for error flag            */
char *input_buffer_ptr;			/* pointer to input buffer           */

  while (1)
  {
  update_simulator_screen ();		/* update the simulator display */
  error_flag = 0;
    for (index = 0; index != sizeof (input_buffer); index++)
    {
    input_buffer[index] = ' ';
    }
  line_in (input_buffer);		/* input a line */
  input_buffer_ptr = input_buffer;	/* initialize ptr to start of buffer */
    if (upper_case (input_buffer[0]) == 'Q')
    {
    break;				/* exit program on quit comment */
    }

/* Convert operand #1, increment input buffer ptr to start of next token */

    if (sscanf (input_buffer_ptr, "%d", &operand1) == 0)
    {
    sprintf (input_buffer, "Input Error\n");
    }
    else
    {
    input_buffer_ptr = next_token (input_buffer_ptr);

/* Skip spaces and tabs and store the operation */

      while (*input_buffer_ptr == ' ' || *input_buffer_ptr == '\t')
      {
      input_buffer_ptr++;
      }
    operation = *input_buffer_ptr++;
      while (*input_buffer_ptr == ' ' || *input_buffer_ptr == '\t')
      {
      input_buffer_ptr++;
      }

/* Convert operand #2, increment input buffer ptr to start of next token */

      if (sscanf (input_buffer_ptr, "%d", &operand2) == 0)
      {
      sprintf (input_buffer, "Input Error\n");
      }
      else
      {
      input_buffer_ptr = next_token (input_buffer_ptr);

/* Perform the operation */

        switch (operation)
        {
        case '+':
          result = operand1 + operand2;
        break;

        case '-':
          result = operand1 - operand2;
        break;

        case '*':
          result = operand1 * operand2;
        break;

        case '/':
          result = operand1 / operand2;
        break;

        default:
          error_flag = 1;
          sprintf (input_buffer, "Illegal Operation\n");
        break;
        }
      }

/* Convert and display the result */

      if (error_flag == 0)
      {
      input_buffer_ptr += sprintf (input_buffer_ptr, " = %d  ", result);
      }
    }
    for (index = 0; index != sizeof (result_buffer); index++)
    {
    result_buffer[index] = ' ';
    }
  strcpy (result_buffer, input_buffer);
  }
}

/* Input a line up to a carriage return. */

void line_in (buffer_ptr)
char *buffer_ptr;			/* ptr to character storage buffer */
{
char c1;				/* scratchpad character            */

  while ((c1 = *((char *) RECEIVER)) != '\r')
  {
  *buffer_ptr++ = c1;			/* store character in buffer       */
  }
*buffer_ptr++ = '\n';			/* terminate character string      */
*buffer_ptr = 0;
}

/* Find the start of the next token */

char *next_token (string_ptr)
char *string_ptr;
{
  if (*string_ptr == '-')
  {
  string_ptr++;				/* skip minus sign */
  }
  while (*string_ptr >= '0' && *string_ptr <= '9')
  {
  string_ptr++;
  }
return (string_ptr);
}

/* Convert character to upper case */

char upper_case (character)
char character;
{
  if (character >= 'a' && character <= 'z')
  {
  character -= 0x20;
  }
return (character);
}

/* The only purpose for this routine is to provide a global address that    */
/* can be used with the simulator 'sua' command.  This command updates      */
/* the simulator screen when the code at the specified address is executed. */

update_simulator_screen ()
{
}





